<?php
/**
 * @title 847. 访问所有节点的最短路径 - 递归+剪枝
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[][] $graph
     * @return Integer
     */
    function shortestPathLength($graph) {
        /**
         * @since 2021-08-06 长度
         */
        $this->len = count($graph);

        /**
         * @since 2021-08-06 最短路径
         */
        $this->minRoute = PHP_INT_MAX;

        /**
         * @since 2021-08-06 单点连接线的最小数
         */
        $minlen = PHP_INT_MAX;
        for($i=0; $i<$this->len; $i++){
            $minlen = min($minlen, count($graph[$i]));
        }

        /**
         * @since 2021-08-06 只尝试连接线最小数的单点
         */
        for($i=0; $i<$this->len; $i++){
            if(count($graph[$i]) == $minlen){
                // echo $i, PHP_EOL;
                self::find($graph, [], 0, [], $i);
            } else {}
        }

        /**
         * @return
         */
        return $this->minRoute;
    }

    /**
     * @title 找经过所有点的最短路径
     * @author start2004
     * @since 2021-08-06 Created
     *
     * @param array $graph
     * @param array $arriveArray
     * @param int $arriveSum
     * @param array $routeArray
     * @param int $node
     */
    public function find($graph = [], $arriveArray = [], $arriveSum = 0, $routeArray = [], $node = 0){
        /**
         * @since 2021-08-06 新的途径点
         */
        // $routeArray[] = $node;
        if(isset($arriveArray[$node])){
            $arriveArray[$node] += 1;
        } else {
            $arriveArray[$node] = 1;
        }
        $arriveSum++;
        // echo "node={$node}, rount=", implode(",", $routeArray), PHP_EOL;

        /**
         * @since 2021-08-06 是否已经经过所有点
         */
        $arriveCnt = count($arriveArray);
        if($arriveCnt == $this->len){
            // echo "经过所有点, 路径长度=", array_sum($arriveArray)-1, PHP_EOL;

            /**
             * @since 2021-08-06 更新最短路径
             */
            // $this->minRoute = min($this->minRoute, $arriveSum-1);
            $this->minRoute = $arriveSum-1;
        } else {}

        /**
         * @since 2021-08-06 剪枝, 途径点后续必然 +(len-arriveCnt), 必须小于minRoute-(len-arriveCnt)
         */
        if($arriveSum-1 >= $this->minRoute-($this->len-$arriveCnt)){
            /**
             * @since 2021-08-06 不可能更短路径
             */
            return false;
        } else {}
        
        /**
         * @since 2021-08-06 可以到达的点
         */
        foreach ($graph[$node] as $key=>$next){
            $nextGraph = $graph;
            unset($nextGraph[$node][$key]);

            self::find($nextGraph, $arriveArray, $arriveSum, $routeArray, $next);
        }
    }
}

/**
 * @url http://127.0.0.1/leetcode/202108/2021.08.06.php
 */
$datas = [
    [[[1,2,3],[0],[0],[0]]], // 4
    [[[1],[0,2,4],[1,3,4],[2],[1,2]]], // 4
    [[[2,10],[2,7],[0,1,3,4,5,8],[2],[2],[2],[8],[9,11,8,1],[7,6,2],[7],[11,0],[7,10]]], // 15
    [[[1],[0,2,4],[1,3],[2],[1,5],[4]]], // 6
    [[[1,2,3,4,5,6,7,8,9,10,11],[0,2,5,6,8],[0,1,4,5,6,9,10,11],[0,4,5,6,8,9,10,11],[0,2,3,5,6,8,10],[0,1,2,3,4,6,8,9,10,11],[0,1,2,3,4,5,8,10,11],[0,8],[0,1,3,4,5,6,7,9,10,11],[0,2,3,5,8,10],[0,2,3,4,5,6,8,9],[0,2,3,5,6,8]]], //
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->shortestPathLength(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
